package com.hy.main.demo.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.nio.charset.StandardCharsets;

/**
 * @Author yang DongYong
 * @Date 2022/5/9 - 05 - 09 - 0:24
 * @Description: com.hy.main.demo.rabbitmq
 * @version: 1.0
 */
public class Worker03 {
    private static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("A1处理消息的时间较短");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            SleepUtils.sleep(1);
            System.out.println("A1接收到的消息:" + new String(message.getBody(), "UTF-8"));
            /**
             * 1.消息的标记 tag
             * 2.是否批量应答 false不进行批量应答 true进行批量应答
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        //  0为轮询 1为不公平分发 2为预取值，意思就是这个队列可以存我们设置的消息大小，然后停止接收消息
        channel.basicQos(2);
        // 采用手动应答
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME, autoAck, deliverCallback, (consumerTag -> System.out.println("消费者取消消费")));
    }
}

 class Worker04 {
    private static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        System.out.println("A2处理消息的时间较长");
        Channel channel = RabbitMqUtils.getChannel();
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            SleepUtils.sleep(30);
            System.out.println("A2接收到的消息:" + new String(message.getBody(), StandardCharsets.UTF_8));
            /**
             * 1.消息的标记 tag
             * 2.是否批量应答 false不进行批量应答 true进行批量应答
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        // 设置不公平分发  0为轮询 1为不公平分发 2为预取值，意思就是这个队列可以存我们设置的消息大小，然后停止接收消息
        channel.basicQos(5);
        // 采用手动应答
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME, autoAck, deliverCallback, (consumerTag -> System.out.println("消费者取消消费")));
    }
}
